WScript.Echo("\n\n===============================================================");
WScript.Echo(" wmp-db-exporter Toolkit version 0.0.4");
WScript.Echo(" http://code.google.com/p/wmp-db-exporter/")
WScript.Echo("===============================================================\n");

var fso, exportFile, encoding;
fso = new ActiveXObject("Scripting.FileSystemObject");
exportFile = fso.CreateTextFile("dump.sql", true, -1);

var Player = new ActiveXObject("WMPlayer.OCX.7");
var playlist = Player.mediaCollection.getAll();
var item = null;

WScript.StdOut.Write("\nFound "+playlist.count+" items.\n\nExporting, please wait ...");
var exportCount = 0;
var skipCount = 0;
var failCount = 0;

for (var i = 0; i < playlist.count; i++) {
    if (playlist.item(i).getItemInfo("MediaType") == "audio") {
            item = playlist.item(i);
        
            var rating = 0; 
            switch (parseInt(item.getItemInfo("UserRating"))) {
                case 99:
                    rating = 5;
                    break;
                case 75:
                    rating = 4;
                    break;
                case 50:
                    rating = 3;
                    break;
                case 25:
                    rating = 2;
                    break;
                case 1:
                    rating = 1;
                    break;
            }
            var drive = item.getItemInfo("SourceURL").charAt(0);        
            var fileName = item.getItemInfo("SourceURL").replace(drive + ":", "");
            var exportLine = "UPDATE MAIN SET MARK=" + rating + " WHERE sFileName=\"" + fileName + "\" AND sDrive=\"" + drive + "\";";
            
            try {
    			exportFile.WriteLine(exportLine);
                WScript.StdOut.Write(".");
                exportCount++;
            } catch(e){
                failCount++;
                WScript.Echo("!!!! ERROR: Failed writing \""+exportLine+"\" to file");
            }
    } else {
        skipCount++;
    }
}

WScript.Echo("\n\nFINISHED: Successfully exported "+exportCount+" items, skipped "+skipCount+" non-audio files, failed exporting "+failCount+" items.\n\n");

exportFile.Close();